package com.ss.gReader.controller;

import com.sun.javafx.tk.FontLoader;
import com.sun.javafx.tk.FontMetrics;
import com.sun.javafx.tk.Toolkit;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class MainController implements Initializable {
    public Label labelText;
    public VBox mbox;
    private ReadOnlyDoubleProperty boxWidth;
    private ReadOnlyDoubleProperty boxHeight;

    private String content = "好".repeat(10000);

    private float w;
    private float h;
    private int len;
    private int startNum = 0;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        labelText.setFont(Font.font(60));
        FontLoader fontLoader = Toolkit.getToolkit().getFontLoader();
        FontMetrics metrics = fontLoader.getFontMetrics(labelText.getFont());
        h = metrics.getLineHeight();
        w = fontLoader.getCharWidth('好', labelText.getFont());
        boxWidth = mbox.widthProperty();
        boxHeight = mbox.heightProperty();
        boxWidth.addListener((observableValue, number, t1) -> changeSize());
        boxHeight.addListener((observableValue, number, t1) -> changeSize());
        setContent();
    }

    private void setContent() {
        int rows = (int) (boxWidth.get() / w);
        int lines = (int) (boxHeight.get() / (h + 2));
        len =  rows * lines;
        labelText.setText(content.substring(startNum, Math.min(len, content.length())));
    }

    private void changeSize() {
        setContent();
    }

}
